package com.hsogoo.study.springapplication;

import com.hsogoo.study.springapplication.domain.Person;
import com.hsogoo.study.springapplication.domain.Student;
import com.hsogoo.study.springapplication.group.CheckAge;
import com.hsogoo.study.springapplication.group.CheckName;
import org.hibernate.validator.HibernateValidator;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import javax.validation.groups.Default;
import java.util.Set;

/**
 * @Author huangsaigang
 * Created @2019-10-16.
 */
public class ValidationTest {

    public static void main(String[] args) {

        ValidatorFactory validatorFactory = Validation.byProvider( HibernateValidator.class )
                .configure()
                .addProperty( "hibernate.validator.fail_fast", "false" )
                .buildValidatorFactory();
        Validator validator = validatorFactory.getValidator();

        Student student = new Student();
//        student.setName("hsogoo");
//        student.setAge(10);
        student.setClassName("三（3）班");
//        student.setSchoolName("新金童小学");

        Set<ConstraintViolation<Student>> validate = validator.validate(student,CheckAge.class, Default.class);
        System.out.println(validate.iterator().next().getMessage());
        for (ConstraintViolation<Student> item : validate) {
            System.out.println(item.getMessage());
        }
    }
}
